package com.lemon.smartsafety.ui.activity.fire.item.statistics;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.http.HttpBack;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.BaseMode;
import com.lemon.smartsafety.result.fire.FireCensusInfo;
import com.lemon.smartsafety.result.fire.FireDate;
import com.lemon.smartsafety.result.fire.SuperviseCompanyResult;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.ui.activity.fire.FirefightingUtil;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.TimeUtil;
import com.lemon.smartsafety.view.ViewFireTop;

import java.util.List;

import butterknife.Bind;
import cn.aigestudio.datepicker.bizs.calendars.DPCManager;
import cn.aigestudio.datepicker.bizs.decors.DPDecor;
import cn.aigestudio.datepicker.bizs.themes.DPFireCalendarTheme;
import cn.aigestudio.datepicker.bizs.themes.DPTManager;
import cn.aigestudio.datepicker.cons.DPMode;
import cn.aigestudio.datepicker.views.DatePicker;

public class CalendarStatisticsActivity extends NewBaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.fireTop)
    ViewFireTop fireTop;
    @Bind(R.id.datePicker)
    DatePicker datePicker;
    @Bind(R.id.txt_place)
    TextView txtPlace;
    @Bind(R.id.txt_state)
    TextView txtState;
    @Bind(R.id.txt_time)
    TextView txtTime;


    private DPTManager dptManager;
    private DPCManager dpcManager;


    private FireCensusInfo fireCensusInfo;
    private SuperviseCompanyResult.ResultEntity resultEntity;


    private List<FireDate> fireDateList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            fireCensusInfo = (FireCensusInfo) getIntent().getSerializableExtra(BundleKey.FIRE_GENSUS_TAG);
            resultEntity = (SuperviseCompanyResult.ResultEntity) getIntent().getSerializableExtra(BundleKey.FIRE_COMPANY_BASE_INFO);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_calendar_statistics;
    }

    @Override
    protected void initTitleView() {
        setBarTitle("当月户籍化上报");
    }

    @Override
    protected void initView() {
        fireTop.setTitleContent(resultEntity.getCompanyName());
        fireTop.setTitleSubContent(fireCensusInfo.getYear()+"年"+fireCensusInfo.getMonth()+"月");
        fireTop.setNum1Content(fireCensusInfo.getReportCount());
        fireTop.setNum2Content("/"+fireCensusInfo.getDayCount());
        fireTop.setNum1Color(FirefightingUtil.getScoreTextColor(fireCensusInfo.getStatus()));
        fireTop.setNum2Color(R.color.colorAppBlue);

        try {
            initDate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initDate() {

        dptManager=new DPTManager();
        dptManager.initCalendar(new DPFireCalendarTheme());
        datePicker.setmTManager(dptManager);

        datePicker.setDate(Integer.valueOf(fireCensusInfo.getYear()),Integer.valueOf(fireCensusInfo.getMonth()));
        datePicker.setModeNone(DPMode.NONE);
        datePicker.setScorll(false);
        datePicker.setTodayDisplay(false);
        datePicker.setIsScorll(false);


    }

    @Override
    protected void initHttp() {
        httpCalendar();
    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }


    private void  httpCalendar(){
        if (!bPutHttp) {
            bPutHttp = true;
            LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");

            params.put("company_code", resultEntity.getCompanyCode());
            params.put("year", fireCensusInfo.getYear());
            params.put("month", fireCensusInfo.getMonth());


            showLoading("");
            HttpOkBase.httpBasePost(params, HttpPath.getmBase(HttpPath.Get_census_detail_info), "", new HttpOkBase.HttpInterface() {
                @Override
                public void callBackFunction(boolean httpClient, String result) {

                    dismissLoading();
                    bPutHttp = false;
                    if (httpClient) {
                        BaseMode baseMode = JsonUtils.fromJson(result, BaseMode.class);
                        if (!CommonUtil.objIsEmpty(baseMode)) {
                            if (HttpBack.httpBackError(baseMode)) {

                                try {
                                    fireDateList =mGson.fromJson(mGson.toJson(baseMode.getResult()), new TypeToken<List<FireDate>>() {
                                    }.getType());
                                    refreshCalendar();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            } else {
                                strShowMsg = baseMode.getMsg();
                                handler.sendEmptyMessage(SHOW_MSG);
                            }
                        }
                    }
                }
            });
        }

    }


    private void refreshCalendar(){

        if(CommonUtil.listIsEmpty(fireDateList)){
            return;
        }


        dpcManager=new DPCManager();
        dpcManager.setDecorClearBG();
        dpcManager.setDecorBG(TimeUtil.getAllTheDateOftheMonthStr(TimeUtil.tranTimeForDate(fireDateList.get(0).getDay())));
        datePicker.setmCManager(dpcManager);


       // DPTManager.getInstance().initCalendar(new DPFireCalendarTheme());
        datePicker.setDate(Integer.valueOf(fireCensusInfo.getYear()),Integer.valueOf(fireCensusInfo.getMonth()));
        datePicker.setModeNone(DPMode.NONE);
        datePicker.setDPDecor(new DPDecor() {
            @Override
            public void drawDecorBG(Canvas canvas, Rect rect, Paint paint, String data) {
                super.drawDecorBG(canvas, rect, paint, data);

                for(int i=0;i<fireDateList.size();i++){
                    if(data.equals(TimeUtil.transCalendarDatePicker(fireDateList.get(i).getDay()))){
                        paint.setColor(ContextCompat.getColor(mActivity, FirefightingUtil.getCalendarColor(fireDateList.get(i).getStatus())));
                        canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2, paint);
                        break;
                    }
                }
            }


        });
        datePicker.setScorll(false);
        datePicker.setTodayDisplay(false);
        datePicker.setIsScorll(false);
    }



}
